<?php
defined('SYSPATH') or die('No direct script access.');

class Model_TransactionHistory extends ORM{

	protected $_table_name  = 'transaction_history';
	protected $_table_names_plural = false;

	public function searchAction($desc){
		$this->insert($desc, "search");
	}
	
	public function scanAction($desc){
		$this->insert($desc, "scan");
	}
	
	public function insert($desc,$type){
		if (!$this->is_Utf8($desc)){
			$desc = mb_convert_encoding($desc, "utf-8", "gb2312");
		}
		$this->desc = $desc;
		$this->type = $type;
		$this->createtime = Date::formatted_time();

		return $this->save();
	}

	private function is_Utf8($value)
	{
		if (preg_match("/^([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}/",$value) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}$/",$value) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){2,}/",$value) == true)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}